package own.stu.jobgib.playown.io.fileRead;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;

import static own.stu.jobgib.playown.io.fileRead.Constant.FILE_NAME;

@Slf4j
public class GenerateData {

    public static void main(String[] args) throws IOException {
        File file = new File("/Users/qim/Desktop/bili/510805858/1-故 乡 秋 天.jpg");
        System.out.println(file.exists());
    }

    public static void main2(String[] args) throws IOException {
        File file = new File(FILE_NAME);
        if (file.exists()) {
            file.delete();
        }
        // cost : 59.51 s
        // generateData(1 * 1024 * 1024 * 1024);

        // 100000 一行，write, cost :  32.82 s || 36.94 s
        //generateData_ii(1 * 1024 * 1024 * 1024);


        /*StringBuilder sb = new StringBuilder();
        for (int k = 0; k < 10; k++) {
            sb.append(k + ",");
        }
        byte[] bs = sb.toString().getBytes();
        System.out.println(new String(bs)); // 0,1,2,3,4,5,6,7,8,9,
        System.out.println(" =========== ");
        byte[] commaBytes = ",".getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(line_size * 32);
        for (int k = 0; k < 10; k++) {
            buffer.putInt(k);
            buffer.put(commaBytes);
        }
        byte[] bytes = bytebuffer2ByteArray(buffer);
        System.out.println(new String(bytes));*/
    }

    /**
     * 产生10G的 1-1000的数据在D盘
     */
    public static void generateData_ii(long count) throws IOException {
        File file = new File(FILE_NAME);
        if (!file.exists()) {
            file.createNewFile();
        }

        long offset = 0;
        RandomAccessFile accessFile = null;
        FileChannel fileChannel = null;
        try {
            accessFile = new RandomAccessFile(file, "rw");
            fileChannel = accessFile.getChannel();

            Stopwatch watch = Stopwatch.createStarted();
            /*ByteBuffer buffer = ByteBuffer.allocate(line_size * 32);
            byte[] commaBytes = ",".getBytes();
            long circle = Math.min(line_size, count);
            for (int i = 1; i <= count; ) {
                for (int j = 0; j < circle; j++) {
                    buffer.putInt(generateRandomData(15, 100));
                    buffer.put(commaBytes);
                }
                fileChannel.write(buffer);
            *//*MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, offset, buffer.position());
            mappedByteBuffer.put();*//*
                buffer.flip();
                i += circle;
            }*/

            long circle = Math.min(line_size, count);
            for (int i = 1; i <= count; ) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < circle; j++) {
                    sb.append(generateRandomData(15, 100)).append(",");
                }
                sb.append("\n");
                byte[] bs = sb.toString().getBytes();
                MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, offset, bs.length);
                mappedByteBuffer.put(bs);
                i += circle;
                offset = offset + bs.length;
            }

            watch.stop();
            log.info("time: {}", watch);
        } finally {
            if (fileChannel != null) {
                fileChannel.close();
            }
            if (accessFile != null) {
                accessFile.close();
            }
        }

    }

    private static int line_size = 100000;

    public static void generateData(long byteSize) throws IOException {
        File file = new File(FILE_NAME);
        if (!file.exists()) {
            file.createNewFile();
        }

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
        Stopwatch watch = Stopwatch.createStarted();
        for (int i = 1; i <= byteSize; i++) {
            writer.write(generateRandomData(15, 100) + ",");
            // 10,000行数据换一行
            if (i % line_size == 0) {
                writer.write("\n");
            }
        }
        watch.stop();
        log.info("time: {}", watch);
        writer.close();
    }

    private static Random random = new Random();

    public static int generateRandomData(int start, int end) {
        return random.nextInt(end - start + 1) + start;
    }
}
